Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports System.Collections.Specialized
Imports Raccoom.Windows.Forms
Imports System.Environment
Imports Cryptor

Public Class Form1
  Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

  Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call

  End Sub

  'Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub

  'Required by the Windows Form Designer
  Private components As System.ComponentModel.IContainer

  'NOTE: The following procedure is required by the Windows Form Designer
  'It can be modified using the Windows Form Designer.  
  'Do not modify it using the code editor.
  Friend WithEvents btnUnzip As System.Windows.Forms.Button
  Friend WithEvents StatusStrip1 As System.Windows.Forms.StatusStrip
  Friend WithEvents tsPB1 As System.Windows.Forms.ToolStripProgressBar
  Friend WithEvents tsSB1 As System.Windows.Forms.ToolStripStatusLabel
  Friend WithEvents bkgWork1 As System.ComponentModel.BackgroundWorker
  Friend WithEvents txtFileMask As System.Windows.Forms.TextBox
  Friend WithEvents Label3 As System.Windows.Forms.Label
  Friend WithEvents Label4 As System.Windows.Forms.Label
  Friend WithEvents cbRenameType As System.Windows.Forms.ComboBox
  Friend WithEvents chkRenameFolders As System.Windows.Forms.CheckBox
  Friend WithEvents Label5 As System.Windows.Forms.Label
  Friend WithEvents udStartNumber As System.Windows.Forms.NumericUpDown
  Friend WithEvents ToolTip1 As System.Windows.Forms.ToolTip
  Friend WithEvents treeFolders As Raccoom.Windows.Forms.TreeViewFolderBrowser
  Friend WithEvents chkRecursiveCheck As System.Windows.Forms.CheckBox
  Friend WithEvents Label7 As System.Windows.Forms.Label
  Friend WithEvents cbCheckDepth As System.Windows.Forms.ComboBox
  Friend WithEvents btnStart As System.Windows.Forms.Button
  Friend WithEvents ContextMenuStripForNodes As System.Windows.Forms.ContextMenuStrip
  Friend WithEvents ToolStripMenuItemExpandAll As System.Windows.Forms.ToolStripMenuItem
  Friend WithEvents ToolStripMenuItemCollapseAll As System.Windows.Forms.ToolStripMenuItem
  Friend WithEvents Label1 As System.Windows.Forms.Label
  Friend WithEvents ToolStripMenuItemRefresh As System.Windows.Forms.ToolStripMenuItem
  Friend WithEvents udNumDigits As System.Windows.Forms.NumericUpDown
  Friend WithEvents btnEncrypt As System.Windows.Forms.Button
  Friend WithEvents btnDecrypt As System.Windows.Forms.Button
  Friend WithEvents LinkLabel1 As System.Windows.Forms.LinkLabel
  Friend WithEvents cbFileMaskType As System.Windows.Forms.ComboBox
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.components = New System.ComponentModel.Container
    Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
    Me.btnUnzip = New System.Windows.Forms.Button
    Me.StatusStrip1 = New System.Windows.Forms.StatusStrip
    Me.tsSB1 = New System.Windows.Forms.ToolStripStatusLabel
    Me.tsPB1 = New System.Windows.Forms.ToolStripProgressBar
    Me.bkgWork1 = New System.ComponentModel.BackgroundWorker
    Me.Label3 = New System.Windows.Forms.Label
    Me.Label4 = New System.Windows.Forms.Label
    Me.cbRenameType = New System.Windows.Forms.ComboBox
    Me.Label5 = New System.Windows.Forms.Label
    Me.udStartNumber = New System.Windows.Forms.NumericUpDown
    Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
    Me.txtFileMask = New System.Windows.Forms.TextBox
    Me.chkRecursiveCheck = New System.Windows.Forms.CheckBox
    Me.Label7 = New System.Windows.Forms.Label
    Me.cbCheckDepth = New System.Windows.Forms.ComboBox
    Me.btnStart = New System.Windows.Forms.Button
    Me.ContextMenuStripForNodes = New System.Windows.Forms.ContextMenuStrip(Me.components)
    Me.ToolStripMenuItemExpandAll = New System.Windows.Forms.ToolStripMenuItem
    Me.ToolStripMenuItemCollapseAll = New System.Windows.Forms.ToolStripMenuItem
    Me.ToolStripMenuItemRefresh = New System.Windows.Forms.ToolStripMenuItem
    Me.treeFolders = New Raccoom.Windows.Forms.TreeViewFolderBrowser
    Me.Label1 = New System.Windows.Forms.Label
    Me.cbFileMaskType = New System.Windows.Forms.ComboBox
    Me.udNumDigits = New System.Windows.Forms.NumericUpDown
    Me.btnEncrypt = New System.Windows.Forms.Button
    Me.btnDecrypt = New System.Windows.Forms.Button
    Me.chkRenameFolders = New System.Windows.Forms.CheckBox
    Me.LinkLabel1 = New System.Windows.Forms.LinkLabel
    Me.StatusStrip1.SuspendLayout()
    CType(Me.udStartNumber, System.ComponentModel.ISupportInitialize).BeginInit()
    Me.ContextMenuStripForNodes.SuspendLayout()
    CType(Me.udNumDigits, System.ComponentModel.ISupportInitialize).BeginInit()
    Me.SuspendLayout()
    '
    'btnUnzip
    '
    resources.ApplyResources(Me.btnUnzip, "btnUnzip")
    Me.btnUnzip.Name = "btnUnzip"
    '
    'StatusStrip1
    '
    Me.StatusStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.tsSB1, Me.tsPB1})
    resources.ApplyResources(Me.StatusStrip1, "StatusStrip1")
    Me.StatusStrip1.Name = "StatusStrip1"
    '
    'tsSB1
    '
    Me.tsSB1.Name = "tsSB1"
    resources.ApplyResources(Me.tsSB1, "tsSB1")
    Me.tsSB1.Spring = True
    '
    'tsPB1
    '
    Me.tsPB1.Name = "tsPB1"
    resources.ApplyResources(Me.tsPB1, "tsPB1")
    Me.tsPB1.Step = 1
    '
    'bkgWork1
    '
    Me.bkgWork1.WorkerReportsProgress = True
    Me.bkgWork1.WorkerSupportsCancellation = True
    '
    'Label3
    '
    resources.ApplyResources(Me.Label3, "Label3")
    Me.Label3.Name = "Label3"
    '
    'Label4
    '
    resources.ApplyResources(Me.Label4, "Label4")
    Me.Label4.Name = "Label4"
    '
    'cbRenameType
    '
    Me.cbRenameType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
    Me.cbRenameType.FormattingEnabled = True
    Me.cbRenameType.Items.AddRange(New Object() {resources.GetString("cbRenameType.Items"), resources.GetString("cbRenameType.Items1")})
    resources.ApplyResources(Me.cbRenameType, "cbRenameType")
    Me.cbRenameType.Name = "cbRenameType"
    '
    'Label5
    '
    resources.ApplyResources(Me.Label5, "Label5")
    Me.Label5.Name = "Label5"
    '
    'udStartNumber
    '
    resources.ApplyResources(Me.udStartNumber, "udStartNumber")
    Me.udStartNumber.Maximum = New Decimal(New Integer() {-1530494976, 232830, 0, 0})
    Me.udStartNumber.Name = "udStartNumber"
    '
    'txtFileMask
    '
    Me.txtFileMask.DataBindings.Add(New System.Windows.Forms.Binding("Text", Global.MassEncrypt.My.MySettings.Default, "txtFileMask", True, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))
    resources.ApplyResources(Me.txtFileMask, "txtFileMask")
    Me.txtFileMask.Name = "txtFileMask"
    Me.txtFileMask.Text = Global.MassEncrypt.My.MySettings.Default.txtFileMask
    Me.ToolTip1.SetToolTip(Me.txtFileMask, resources.GetString("txtFileMask.ToolTip"))
    '
    'chkRecursiveCheck
    '
    resources.ApplyResources(Me.chkRecursiveCheck, "chkRecursiveCheck")
    Me.chkRecursiveCheck.Checked = Global.MassEncrypt.My.MySettings.Default.chkRecursiveCheck
    Me.chkRecursiveCheck.CheckState = System.Windows.Forms.CheckState.Checked
    Me.chkRecursiveCheck.DataBindings.Add(New System.Windows.Forms.Binding("Checked", Global.MassEncrypt.My.MySettings.Default, "chkRecursiveCheck", True, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))
    Me.chkRecursiveCheck.Name = "chkRecursiveCheck"
    Me.chkRecursiveCheck.UseVisualStyleBackColor = True
    '
    'Label7
    '
    resources.ApplyResources(Me.Label7, "Label7")
    Me.Label7.Name = "Label7"
    '
    'cbCheckDepth
    '
    Me.cbCheckDepth.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
    Me.cbCheckDepth.FormattingEnabled = True
    resources.ApplyResources(Me.cbCheckDepth, "cbCheckDepth")
    Me.cbCheckDepth.Name = "cbCheckDepth"
    '
    'btnStart
    '
    resources.ApplyResources(Me.btnStart, "btnStart")
    Me.btnStart.Name = "btnStart"
    Me.btnStart.UseVisualStyleBackColor = True
    '
    'ContextMenuStripForNodes
    '
    Me.ContextMenuStripForNodes.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ToolStripMenuItemExpandAll, Me.ToolStripMenuItemCollapseAll, Me.ToolStripMenuItemRefresh})
    Me.ContextMenuStripForNodes.Name = "ContextMenuStrip1"
    resources.ApplyResources(Me.ContextMenuStripForNodes, "ContextMenuStripForNodes")
    '
    'ToolStripMenuItemExpandAll
    '
    resources.ApplyResources(Me.ToolStripMenuItemExpandAll, "ToolStripMenuItemExpandAll")
    Me.ToolStripMenuItemExpandAll.Name = "ToolStripMenuItemExpandAll"
    '
    'ToolStripMenuItemCollapseAll
    '
    resources.ApplyResources(Me.ToolStripMenuItemCollapseAll, "ToolStripMenuItemCollapseAll")
    Me.ToolStripMenuItemCollapseAll.Name = "ToolStripMenuItemCollapseAll"
    '
    'ToolStripMenuItemRefresh
    '
    Me.ToolStripMenuItemRefresh.Image = Global.MassEncrypt.My.Resources.Resources.Tray_93
    Me.ToolStripMenuItemRefresh.Name = "ToolStripMenuItemRefresh"
    resources.ApplyResources(Me.ToolStripMenuItemRefresh, "ToolStripMenuItemRefresh")
    '
    'treeFolders
    '
    resources.ApplyResources(Me.treeFolders, "treeFolders")
    Me.treeFolders.ContextMenuStripHandlers = Nothing
    Me.treeFolders.ContextMenuStripStandard = Nothing
    Me.treeFolders.DataSource = Nothing
    Me.treeFolders.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawText
    Me.treeFolders.DriveTypes = CType(((((Raccoom.Windows.Forms.DriveTypes.RemovableDisk Or Raccoom.Windows.Forms.DriveTypes.LocalDisk) _
                Or Raccoom.Windows.Forms.DriveTypes.NetworkDrive) _
                Or Raccoom.Windows.Forms.DriveTypes.CompactDisc) _
                Or Raccoom.Windows.Forms.DriveTypes.RAMDisk), Raccoom.Windows.Forms.DriveTypes)
    Me.treeFolders.HideSelection = False
    Me.treeFolders.MaxCheckDepth = 0
    Me.treeFolders.Name = "TreeView1"
    Me.treeFolders.SelectedDirectories = CType(resources.GetObject("treeFolders.SelectedDirectories"), System.Collections.Specialized.StringCollection)
    Me.treeFolders.SelectedFolderPath = Nothing
    Me.treeFolders.StartCheckDepth = 0
    '
    'Label1
    '
    resources.ApplyResources(Me.Label1, "Label1")
    Me.Label1.Name = "Label1"
    '
    'cbFileMaskType
    '
    Me.cbFileMaskType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
    Me.cbFileMaskType.FormattingEnabled = True
    resources.ApplyResources(Me.cbFileMaskType, "cbFileMaskType")
    Me.cbFileMaskType.Name = "cbFileMaskType"
    '
    'udNumDigits
    '
    resources.ApplyResources(Me.udNumDigits, "udNumDigits")
    Me.udNumDigits.Maximum = New Decimal(New Integer() {1000000000, 0, 0, 0})
    Me.udNumDigits.Name = "udNumDigits"
    Me.udNumDigits.Value = New Decimal(New Integer() {4, 0, 0, 0})
    '
    'btnEncrypt
    '
    resources.ApplyResources(Me.btnEncrypt, "btnEncrypt")
    Me.btnEncrypt.Name = "btnEncrypt"
    Me.btnEncrypt.UseVisualStyleBackColor = True
    '
    'btnDecrypt
    '
    resources.ApplyResources(Me.btnDecrypt, "btnDecrypt")
    Me.btnDecrypt.Name = "btnDecrypt"
    Me.btnDecrypt.UseVisualStyleBackColor = True
    '
    'chkRenameFolders
    '
    resources.ApplyResources(Me.chkRenameFolders, "chkRenameFolders")
    Me.chkRenameFolders.Checked = Global.MassEncrypt.My.MySettings.Default.chkRenameFolders
    Me.chkRenameFolders.DataBindings.Add(New System.Windows.Forms.Binding("Checked", Global.MassEncrypt.My.MySettings.Default, "chkRenameFolders", True, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))
    Me.chkRenameFolders.Name = "chkRenameFolders"
    Me.chkRenameFolders.UseVisualStyleBackColor = True
    '
    'LinkLabel1
    '
    resources.ApplyResources(Me.LinkLabel1, "LinkLabel1")
    Me.LinkLabel1.Name = "LinkLabel1"
    Me.LinkLabel1.TabStop = True
    '
    'Form1
    '
    resources.ApplyResources(Me, "$this")
    Me.Controls.Add(Me.LinkLabel1)
    Me.Controls.Add(Me.udNumDigits)
    Me.Controls.Add(Me.cbFileMaskType)
    Me.Controls.Add(Me.treeFolders)
    Me.Controls.Add(Me.StatusStrip1)
    Me.Controls.Add(Me.btnDecrypt)
    Me.Controls.Add(Me.btnStart)
    Me.Controls.Add(Me.btnEncrypt)
    Me.Controls.Add(Me.cbCheckDepth)
    Me.Controls.Add(Me.chkRecursiveCheck)
    Me.Controls.Add(Me.udStartNumber)
    Me.Controls.Add(Me.Label7)
    Me.Controls.Add(Me.cbRenameType)
    Me.Controls.Add(Me.btnUnzip)
    Me.Controls.Add(Me.Label5)
    Me.Controls.Add(Me.chkRenameFolders)
    Me.Controls.Add(Me.Label1)
    Me.Controls.Add(Me.Label3)
    Me.Controls.Add(Me.txtFileMask)
    Me.Controls.Add(Me.Label4)
    Me.Name = "Form1"
    Me.StatusStrip1.ResumeLayout(False)
    Me.StatusStrip1.PerformLayout()
    CType(Me.udStartNumber, System.ComponentModel.ISupportInitialize).EndInit()
    Me.ContextMenuStripForNodes.ResumeLayout(False)
    CType(Me.udNumDigits, System.ComponentModel.ISupportInitialize).EndInit()
    Me.ResumeLayout(False)
    Me.PerformLayout()

  End Sub

#End Region

  Dim WithEvents _renUtils As New RenameUtils
  Dim _wc As WaitCursor
  Dim rootPaths As New StringCollection
  Dim provider As TreeViewFolderBrowserDataProviderShell32
  Dim _passWord As String

  Sub InitLoadFolders()
    ' populate treefolderbrowser control from shell32 provider
    provider = New TreeViewFolderBrowserDataProviderShell32()
    _renUtils.Provider = provider
    provider.EnableContextMenu = True
    provider.ShowFiles = True
    'treeFolders.Nodes.Clear()
    'treeFolders.SelectedDirectories.Clear()
    treeFolders.DataSource = provider
    treeFolders.RootFolder = SpecialFolder.MyComputer
    treeFolders.Populate()
    Dim winfolder, systemfolder As String
    systemfolder = GetFolderPath(SpecialFolder.System)
    Dim diSys As IO.DirectoryInfo = New IO.DirectoryInfo(systemfolder)
    winfolder = diSys.Parent.FullName
    treeFolders.DisabledFolders.Add(GetFolderPath(SpecialFolder.ProgramFiles))
    treeFolders.DisabledFolders.Add(winfolder)
    For Each n As TreeNodePath In treeFolders.Nodes
      rootPaths.Add(n.Path)
    Next
    provider.FileMask = Me.txtFileMask.Text
  End Sub
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' load settings
    Try
      'Dim n As String = Utils.DataProtection.ProtectData("", "Password")
      _renUtils.TreeViewFolderBrowser = treeFolders
      'AddHandler treeFolders.ContextMenuStrip.Opening, AddressOf ContextMenuStripForNodes_Opening
      treeFolders.ContextMenuStripStandard = Me.ContextMenuStripForNodes
      treeFolders.ContextMenuStripHandlers = New EventHandler() { _
       AddressOf ToolStripMenuItemExpandAll_Click, AddressOf ToolStripMenuItemCollapseAll_Click, _
       AddressOf ToolStripMenuItemRefresh_Click}
      Me.txtFileMask.Text = My.Settings.FileMask
      If My.Settings.NumDigits.Length > 0 Then
        udNumDigits.Value = CInt(My.Settings.NumDigits)
      End If
      InitLoadFolders()
      _renUtils.TestMode = False
      tsPB1.Maximum = 100
      adjustInterface()
      '------- name-value lists ------ 
      ' check depth list
      Dim list As NameValueList
      list = New NameValueList
      list.Add(New NameValue(My.Resources.Unlimited, 0))
      For i As Integer = 2 To 10
        list.Add(New NameValue(String.Format(My.Resources.LevelsFS, i), i))
      Next
      list.SetDataSourceOf(cbCheckDepth)
      ' file mask type cb
      list = New NameValueList(My.Resources.FileMaskTypeNV, False)
      list.SetDataSourceOf(Me.cbFileMaskType)
      Me.cbFileMaskType.SelectedIndex = My.Settings.cbFileMaskType
      Me.cbRenameType.SelectedIndex = My.Settings.cbRenameType
      Me.cbCheckDepth.SelectedIndex = My.Settings.cbCheckDepth
    Catch ex As Exception
      MsgBox(ex.Message)
    Finally
      Me.btnUnzip.Focus()
    End Try
  End Sub
  Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    ' save settings
    My.Settings.FileMask = txtFileMask.Text
    My.Settings.NumDigits = udNumDigits.Value
    My.Settings.cbFileMaskType = Me.cbFileMaskType.SelectedIndex
    My.Settings.cbRenameType = Me.cbRenameType.SelectedIndex
    My.Settings.cbCheckDepth = Me.cbCheckDepth.SelectedIndex
    My.Settings.Save()
  End Sub

  Public ReadOnly Property IsWorking() As Boolean
    Get
      Return bkgWork1.IsBusy
    End Get
  End Property

  ' unzip button, asynchronously
  Private Sub btnUnzip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnzip.Click
    RunMassiveWork()
  End Sub
  Private Sub RunMassiveWork()
    If IsWorking Then
      bkgWork1.CancelAsync()
    Else
      '_renUtils.SourceDir = TextBox1.Text
      '_renUtils.TargetDir = TextBox2.Text
      _renUtils.RootFolders = Me.rootPaths
      _renUtils.FileMask = txtFileMask.Text
      _renUtils.RenameFolders = chkRenameFolders.Checked
      _renUtils.RenameFolderType = cbRenameType.SelectedIndex
      _renUtils.NumDigits = CInt(udNumDigits.Value)
      _renUtils.StartNumber = CInt(udStartNumber.Text)
      _renUtils.NumChecked = treeFolders.NumChecked
      ' prepare interface
      bkgWork1.RunWorkerAsync(0)
    End If
    ' uncomment these lines to do syncronous processing
    '_renUtils.RenameAllFilesOfMask()
    adjustInterface()
  End Sub
  Public Sub DoRenameWork()
    Dim frmDlg As New DialogItemsFound
    frmDlg.stringsArray = _renUtils.FileList.ToArray()
    frmDlg.NumFolders = _renUtils.NumFolders
    If frmDlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
      If IsWorking Then
        bkgWork1.CancelAsync()
      Else
        bkgWork1.RunWorkerAsync(1)
      End If
      ' uncomment these lines to do syncronous processing
      '_renUtils.RenameAllFilesOfMask()
      adjustInterface()
    End If
  End Sub
  Private Sub adjustInterface()
    If IsWorking Then
      _wc = New WaitCursor(Me)
    Else
      If _wc IsNot Nothing Then _wc.Dispose() 'set default cursor
    End If
    tsPB1.Visible = IsWorking
    If Not IsWorking Then Me.tsSB1.Text = ""
    btnUnzip.Text = IIf(Not IsWorking, My.Resources.UnzipButton1_1, My.Resources.UnzipButton1_2)
  End Sub

  Private Sub bkgWork1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bkgWork1.DoWork
    ' Get the BackgroundWorker object that raised this event.
    Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
    ' get the zip utils that will do the work
    Dim workType As Integer = CType(e.Argument, Integer)
    _renUtils.Worker = worker
    _renUtils.DweArgs = e
    _renUtils.DoWork(workType)
  End Sub

  Private Sub bkgWork1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bkgWork1.ProgressChanged
    If Me.InvokeRequired Then
      Me.Invoke(New ProgressChangedEventHandler(AddressOf bkgWork1_ProgressChanged), sender, e)
    Else
      Dim arg As RenameUtils.RenameProgressArgs = e.UserState
      tsSB1.Text = arg.Node.Name
      tsPB1.Value = e.ProgressPercentage
      If _renUtils.CurrWorkType = 1 AndAlso arg.Node.IsVisible Then
        arg.Node.Text = arg.NewName
      End If
    End If
    ' arg.Node.EnsureVisible()
  End Sub

  Private Sub bkgWork1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bkgWork1.RunWorkerCompleted
    adjustInterface()
    If e.Cancelled Then
      MessageBox.Show(My.Resources.WorkCanceled, "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
      ' ElseIf MessageBox.Show("Delete files renamed", "Delete files", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
      '   _renUtils.DeleteAllInDir()
    Else
      ' prepared finding files, now do the work
      If _renUtils.CurrWorkType = 0 Then
        Me.DoRenameWork()
      Else
        ' work finished, refresh tree and init utils object
        Me.RefreshNode(treeFolders.SelectedNode)
        _renUtils.Encrypt = False
        _renUtils.Decrypt = False
      End If
    End If
  End Sub

  Private Sub chkRenameFolders_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkRenameFolders.CheckedChanged
    Me.cbRenameType.Enabled = chkRenameFolders.Checked
  End Sub
  Private Sub chkRecursiveCheck_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkRecursiveCheck.CheckedChanged
    Me.treeFolders.CheckboxBehaviorMode = IIf(chkRecursiveCheck.Checked, _
      CheckboxBehaviorMode.RecursiveChecked, CheckboxBehaviorMode.SingleChecked)
    Me.cbCheckDepth.Enabled = chkRecursiveCheck.Checked
  End Sub

  Private Sub cbCheckDepth_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbCheckDepth.SelectedIndexChanged
    Me.treeFolders.MaxCheckDepth = cbCheckDepth.SelectedValue
  End Sub

  ' for test only
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim d1 As New IO.DirectoryInfo("X:\temp\VS2005ImageLibrary\animations")
    d1.MoveTo("X:\temp\VS2005ImageLibrary\animations [00,01]")
  End Sub

  Private Sub txtFileMask_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFileMask.LostFocus
    setMask()
  End Sub

  Sub setMask()
    If provider Is Nothing Then Exit Sub
    provider.FileMask = IIf(cbFileMaskType.SelectedIndex = 0, Nothing, txtFileMask.Text)
    provider.MaskInclude = (cbFileMaskType.SelectedIndex = 1)
    Me.RefreshNode(treeFolders.SelectedNode)
  End Sub

  Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    InitLoadFolders()
  End Sub
  Private Sub ToolStripMenuItemExpandAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItemExpandAll.Click
    treeFolders.BeginUpdate()
    treeFolders.NodeContextMenu.ExpandAll()
    treeFolders.EndUpdate()
  End Sub

  Private Sub ToolStripMenuItemCollapseAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItemCollapseAll.Click
    treeFolders.BeginUpdate()
    treeFolders.NodeContextMenu.Collapse()
    treeFolders.EndUpdate()
  End Sub
  Private Sub ToolStripMenuItemRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItemRefresh.Click
    RefreshNode(treeFolders.NodeContextMenu)
  End Sub
  Sub RefreshNode(ByVal node As TreeNodePath)
    If node Is Nothing Then Exit Sub
    Dim nodeRefreshed As TreeNodePath
    nodeRefreshed = treeFolders.Populate(node.Path)
    If nodeRefreshed IsNot Nothing Then nodeRefreshed.Expand()
  End Sub
  Private Sub cbFileMaskType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbFileMaskType.SelectedIndexChanged
    Me.txtFileMask.Enabled = cbFileMaskType.SelectedIndex > 0
    setMask()
  End Sub

  Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
    Dim frm As New frmSimpleFile()   'FormAskPassword()
    frm.EncryptionWorker.Encrypt = True
    If frm.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
    _renUtils.Encrypt = True
    _renUtils.EncDec.Initialise(frm.Password)
    Me.RunMassiveWork()
  End Sub

  Private Sub btnDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecrypt.Click
    Dim frm As New FormAskPassword()
    If frm.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
    _renUtils.Decrypt = True
    _renUtils.EncDec.Initialise(frm.Password)
    Me.RunMassiveWork()
  End Sub

  Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
    Process.Start("http://masscryptfiles.codeplex.com/")
  End Sub
End Class
